home *** CD-ROM | disk | FTP | other *** search
/ Gekikoh Dennoh Club 5 / Gekikoh Dennoh Club Vol. 5 (Japan).7z / Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin / internet / webx / webxp040.lzh / Source / Image.c < prev    next >
C/C++ Source or Header  |  1998-09-27  |  11KB  |  397 lines

  1. /* Image.c */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <alloca.h>
  8. #include <sys/dos.h>
  9. #include <sys/iocs.h>
  10. #include <sys/xglob.h>
  11. #include "WebXpression.h"
  12. #include "WebCache/WebCache.h"
  13. #include "gifl.h"
  14.  
  15. extern int GetFile (HTTPFILE *);
  16. extern GIFLOAD *gifdecodemain (GIFLOAD * g, char *p);
  17. extern void McPrint (char *);
  18. extern void McDbPrint (char *);
  19. extern void CompressImageHS (void *, void *, unsigned short, unsigned short);
  20. extern void CompressImageHQ (void *, void *, unsigned short, unsigned short);
  21. extern void CompressImage256HS (unsigned short *, unsigned char *,
  22.                 unsigned short *, unsigned short, unsigned short);
  23. extern void CompressImage256HQ (unsigned short *, unsigned char *,
  24.                 unsigned short *, unsigned short, unsigned short);
  25. extern void NonCompressImage256 (unsigned short *, unsigned char *,
  26.                  unsigned short *, unsigned short, unsigned short);
  27. extern void new_B_KEYINP (void), new_B_KEYSNS (void), new_BITSNS (void);
  28. extern void new_CONCTRL (void), new_MS_INIT (void), new_MS_CUROF (void), new_SKEY_MOD (void);
  29. extern IMAGE_LIST *image_list_top, *image_list_end, *image_list_ptr;
  30. extern volatile signed char quit_flag;
  31. extern XPTEXT *disp_xptext;
  32.  
  33. extern unsigned short cache_image;    /* âCâüü[âWâLâââbâVâàÉö */
  34. extern unsigned char image_quality;    /* âCâüü[âWê│ÅkÄ₧é╠ëµÄ┐ */
  35. extern unsigned char image_compress;    /* âCâüü[âWé≡ê│Åké╖éΘé⌐ */
  36.  
  37.  
  38. IMAGE_LIST *image_list_top = NULL, *image_list_end = NULL;
  39. IMAGE_LIST *image_list_ptr = NULL;    /* é▒éΩé⌐éτô╟é▌ì₧é▐â|âCâôâ^ */
  40. static short node = 0;        /* âmü[âhÉö */
  41.  
  42.  
  43. /* ÄwÆΦé│éΩé╜âmü[âhé≡ìφÅ£é╖éΘ */
  44. void DeleteImageNode (IMAGE_LIST * t_ptr)
  45. {
  46.     McDbPrint ("DeleteImageNode() : ");
  47.     McDbPrint (t_ptr->url);
  48.     McDbPrint ("\n");
  49.     if (image_list_top != NULL) {
  50.         if ((t_ptr->data != NULL) && (t_ptr->data != (void *) !NULL))
  51.             _dos_mfree (t_ptr->data);
  52.  
  53.         if (t_ptr->before_ptr == NULL) {
  54.             if (t_ptr->next_ptr == NULL) {
  55.                 /* éPé┬é╡é⌐é╚éóâmü[âhé≡ìφÅ£ */
  56.                 image_list_top = NULL;
  57.                 image_list_end = NULL;
  58.                 image_list_ptr = NULL;
  59.                 free (t_ptr);
  60.             } else {
  61.                 /* Éµô¬é╠âmü[âhé≡ìφÅ£ */
  62.                 image_list_top = t_ptr->next_ptr;
  63.                 (t_ptr->next_ptr)->before_ptr = NULL;
  64.                 free (t_ptr);
  65.             }
  66.         } else {
  67.             if (t_ptr->next_ptr == NULL) {
  68.                 /* ûûö÷é╠âmü[âhé≡ìφÅ£ */
  69.                 image_list_end = t_ptr->before_ptr;
  70.                 (t_ptr->before_ptr)->next_ptr = NULL;
  71.                 free (t_ptr);
  72.             } else {
  73.                 /* Æåè╘é╠âmü[âhé≡ìφÅ£ */
  74.                 (t_ptr->before_ptr)->next_ptr = t_ptr->next_ptr;
  75.                 (t_ptr->next_ptr)->before_ptr = t_ptr->before_ptr;
  76.                 free (t_ptr);
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82.  
  83. /* Éµô¬é╔âmü[âhé≡éPé┬Æ╟ë┴é╖éΘ */
  84. IMAGE_LIST *InsertImageNode (char *url)
  85. {
  86.     IMAGE_LIST *t_ptr;
  87.  
  88.     McDbPrint ("InsertImageNode() : ");
  89.     McDbPrint (url);
  90.     McDbPrint ("\n");
  91.  
  92.     if ((t_ptr = malloc (sizeof (IMAGE_LIST))) == NULL) {
  93.         McDbPrint ("InsertImageNode() : ");
  94.         McPrint ("üª âüâéâèé¬æ½éΦé▄é╣é±\n");
  95.         return (NULL);
  96.     } else {
  97.         if (image_list_top == NULL) {
  98.             /* âmü[âhéOî┬é╠Åèé╔Æ╟ë┴ */
  99.             image_list_top = t_ptr;
  100.             image_list_end = t_ptr;
  101.             image_list_ptr = t_ptr;
  102.             t_ptr->before_ptr = NULL;
  103.             t_ptr->next_ptr = NULL;
  104.         } else {
  105.             /* Éµô¬é╔âmü[âhé≡Æ╟ë┴ */
  106.             image_list_top->before_ptr = t_ptr;
  107.             t_ptr->before_ptr = NULL;
  108.             t_ptr->next_ptr = image_list_top;
  109.             image_list_top = t_ptr;
  110.         }
  111.         t_ptr->x = 0;
  112.         t_ptr->y = 0;
  113.         t_ptr->data = NULL;
  114.         t_ptr->count = 1;
  115.         strcpy (t_ptr->url, url);
  116.     }
  117.  
  118.     node++;
  119.  
  120.     /* âLâââbâVâàé╡é─éóéΘâmü[âh鬠cache_image é≡ëzéªé╜éτûóÄQÅ╞é╠âmü[âhé≡ìφÅ£ */
  121.     if (node > cache_image) {
  122.         IMAGE_LIST *t2_ptr;
  123.         t2_ptr = image_list_end;
  124.         while (t2_ptr != NULL) {
  125.             if ((!t2_ptr->count) && (t2_ptr != image_list_ptr) && (t2_ptr != t_ptr)) {
  126.                 short i;
  127.  
  128.                 /* û{ôûé╔ÄQÅ╞é│éΩé─éóé╚éóé⌐â`âFâbâNüiòsùvé╚é═é╕é╛é¬êΩë₧üj */
  129.                 for (i = 0; i < disp_xptext->image_table_max; i++) {
  130.                     if (t2_ptr == (disp_xptext->image_table)[i].image_list)
  131.                         continue;
  132.                 }
  133.  
  134.                 DeleteImageNode (t2_ptr);
  135.                 node--;
  136.                 break;
  137.             }
  138.             t2_ptr = t2_ptr->before_ptr;
  139.         }
  140.     }
  141.     return (t_ptr);
  142. }
  143.  
  144.  
  145.  
  146. /* url é┼ÄwÆΦé╡é╜âmü[âhé≡îƒì⌡é╖éΘ */
  147. IMAGE_LIST *SearchImageNode (char *url)
  148. {
  149.     IMAGE_LIST *t_ptr = image_list_top;
  150.  
  151.     while (t_ptr != NULL) {
  152.         if (!strcmp (t_ptr->url, url)) {
  153.             t_ptr->count++;
  154.             return (t_ptr);
  155.         }
  156.         t_ptr = t_ptr->next_ptr;
  157.     }
  158.  
  159.     return (NULL);
  160. }
  161.  
  162.  
  163.  
  164. static int LoadGif (HTTPFILE * httpfile)
  165. {
  166.     unsigned char *h = httpfile->content;
  167.     unsigned short xs, ys;    /* î│ëµæ£é╠æσé½é│(Source) */
  168.     unsigned short xd, yd;    /* ôWèJîπëµæ£é╠æσé½é│(Dest) */
  169.     GIFLOAD *g;
  170.     char temp_fname[256];
  171.  
  172.     xs = ((unsigned short) (*(h + 7))) * 256 + (unsigned short) (*(h + 6));
  173.     ys = ((unsigned short) (*(h + 9))) * 256 + (unsigned short) (*(h + 8));
  174.     if (image_compress) {
  175.         if (!(xd = xs / 2))    /* ò¥é¬éPâhâbâgé╛é┴é╜éτ xd = 1 é╔ */
  176.             xd = 1;
  177.         if (!(yd = ys / 2))    /* ìéé│é¬éPâhâbâgé╛é┴é╜éτ yd = 1 é╔ */
  178.             yd = 1;
  179.     } else {
  180.         xd = xs;
  181.         yd = ys;
  182.     }
  183.  
  184.     /* temp_fname é≡ô╛éΘé╛é» */
  185.     WCExist (httpfile, temp_fname);
  186.  
  187.     if ((int) (image_list_ptr->data = _dos_malloc (xd * yd * 2)) < 0) {
  188.         McDbPrint ("LoadGif() : ");
  189.         McPrint ("üª .GIF data ùpâüâéâèé¬æ½éΦé▄é╣é±\n");
  190.         image_list_ptr->data = (void *) !NULL;
  191.         return (-1);
  192.     }
  193.     if ((int) (g = _dos_malloc (sizeof (GIFLOAD))) < 0) {
  194.         McDbPrint ("LoadGif() : ");
  195.         McPrint ("üª .GIF âÅü[âNùpâüâéâèé¬æ½éΦé▄é╣é±\n");
  196.         _dos_mfree (image_list_ptr->data);
  197.         image_list_ptr->data = (void *) !NULL;
  198.         return (-1);
  199.     } {            /* âÅü[âNé≡âNâèâAüié▒é±é╚Æxéóò√û@é┼üEüEüEüj */
  200.         char *p = (char *) g;
  201.         int i;
  202.         for (i = 0; i < sizeof (GIFLOAD); i++)
  203.             *p++ = '\0';
  204.     }
  205.     g->tpcolor = -1;
  206.     g->addr = (void *) -1;
  207.  
  208.     McPrint (".GIF é≡ôWèJé╡é─éóé▄é╖...\n");
  209.     if ((int) (gifdecodemain (g, temp_fname)) < 0) {
  210.         McDbPrint ("LoadGif() : ");
  211.         McPrint ("üª .GIF é╠âfâRü[âhé¬é┼é½é▄é╣é±é┼é╡é╜\n");
  212.         if ((int) g->addr > 0)
  213.             _dos_mfree (g->addr);
  214.         _dos_mfree (g);
  215.         _dos_mfree (image_list_ptr->data);
  216.         image_list_ptr->data = (void *) !NULL;
  217.         return (-1);
  218.     }
  219.     if ((int) g->addr < 0) {
  220.         McDbPrint ("LoadGif() : ");
  221.         McPrint ("üª .GIF ôWèJùpâüâéâèé¬æ½éΦé▄é╣é±\n");
  222.         _dos_mfree (g);
  223.         _dos_mfree (image_list_ptr->data);
  224.         image_list_ptr->data = (void *) !NULL;
  225.         return (-1);
  226.     }
  227.     if (image_compress) {
  228.         if (!image_quality)
  229.             CompressImage256HS (image_list_ptr->data, g->addr, &g->pal_buf[0], xs, ys);
  230.         else
  231.             CompressImage256HQ (image_list_ptr->data, g->addr, &g->pal_buf[0], xs, ys);
  232.         _dos_mfree (g->addr);
  233.         _dos_mfree (g);
  234.     } else {
  235.         NonCompressImage256 (image_list_ptr->data, g->addr, &g->pal_buf[0], xs, ys);
  236.         _dos_mfree (g->addr);
  237.         _dos_mfree (g);
  238.     }
  239.     image_list_ptr->x = xd;
  240.     image_list_ptr->y = yd;
  241.  
  242.  
  243.  
  244.     return (0);
  245. }
  246.  
  247.  
  248. static int LoadJpeg (HTTPFILE * httpfile)
  249. {
  250.     char cmdline[256];
  251.     FILE *fp;
  252.     unsigned short xs = 0, ys = 0;    /* î│ëµæ£é╠æσé½é│(Source) */
  253.     unsigned short xd, yd;    /* ôWèJîπëµæ£é╠æσé½é│(Dest) */
  254.     char temp_fname[256];
  255.     unsigned short *temp_image;
  256.     void *old_CONCTRL, *old_MS_INIT, *old_MS_CUROF, *old_SKEY_MOD;
  257.     char progressive = 0;
  258.  
  259.     /* temp_fname é≡ô╛éΘé╛é» */
  260.     WCExist (httpfile, temp_fname);
  261.  
  262.     /* JPEG âtâ@âCâïé╠ìéé│ü^ëíò¥é≡ô╛éΘ */
  263.     /* vwx é╠â\ü[âXé╠ file.c é≡ÄQìlé╔é╡é▄é╡é╜üiæ╜Ä╙üj */
  264.     if ((fp = fopen (temp_fname, "rb")) != NULL) {
  265.         fseek (fp, 2, SEEK_SET);
  266.         while (!feof (fp)) {
  267.             unsigned char buf[8];
  268.             int c;
  269.  
  270.             if ((c = fgetc (fp)) != 0xff)
  271.                 continue;
  272.             c = fgetc (fp);
  273.             if ((c != 0xc0) && (c != 0xc2)) {
  274.                 c = fgetc (fp) * 256 + fgetc (fp);
  275.                 fseek (fp, c - 2, 1);
  276.                 continue;
  277.             }
  278.             if (c == 0xc2) {    /* âvâìâOâîâbâVâu JPEG üH */
  279.                 progressive = !0;
  280.             }
  281.             fread (buf + 1, 7, sizeof (char), fp);
  282.             ys = *(unsigned short *) (buf + 4);
  283.             xs = *(unsigned short *) (buf + 6);
  284.             break;
  285.         }
  286.         fclose (fp);
  287.     }
  288.     if (!progressive) {
  289.         /* Baseline JPEGüiòüÆ╩é╠ JPEGüjé╠ÅΩìç */
  290.  
  291.         /* JPEGED.R é¬ëµû╩éΓâ}âEâXé≡Åëè·ë╗é╡é╚éóéµéñé╔ */
  292.         old_CONCTRL = _dos_intvcs (0xff23, new_CONCTRL);
  293.         old_MS_INIT = _dos_intvcs (0x170, new_MS_INIT);
  294.         old_MS_CUROF = _dos_intvcs (0x172, new_MS_CUROF);
  295.         old_SKEY_MOD = _dos_intvcs (0x17d, new_SKEY_MOD);
  296.  
  297.         if (image_compress) {
  298.             if ((xd = xs / 2) == 0)    /* ò¥é¬éPâhâbâgé╛é┴é╜éτ xd = 1 é╔ */
  299.                 xd = 1;
  300.             if ((yd = ys / 2) == 0)    /* ìéé│é¬éPâhâbâgé╛é┴é╜éτ yd = 1 é╔ */
  301.                 yd = 1;
  302.         } else {
  303.             xd = xs;
  304.             yd = ys;
  305.         }
  306.  
  307.         if ((int) (temp_image = _dos_malloc (xs * ys * 2)) > 0) {
  308.             sprintf (cmdline, "-VS%d,%d,$%p", xs, ys, temp_image);
  309.             McPrint (".JPG é≡ôWèJé╡é─éóé▄é╖...\n");
  310.             if (!spawnlp (P_WAIT, "JPEGED.R", "JPEGED.R", cmdline, temp_fname, NULL)) {
  311.                 if (image_compress) {
  312.                     if ((int) (image_list_ptr->data = _dos_malloc (xd * yd * 2)) > 0) {
  313.                         if (!image_quality)
  314.                             CompressImageHS (image_list_ptr->data, temp_image, xs, ys);
  315.                         else
  316.                             CompressImageHQ (image_list_ptr->data, temp_image, xs, ys);
  317.                         image_list_ptr->x = xd;
  318.                         image_list_ptr->y = yd;
  319.                     } else {
  320.                         McDbPrint ("LoadJpeg() : ");
  321.                         McPrint ("üª .JPG data ùpâüâéâèé¬æ½éΦé▄é╣é±\n");
  322.                         image_list_ptr->data = (void *) !NULL;
  323.                     }
  324.                 } else {
  325.                     image_list_ptr->data = temp_image;
  326.                     temp_image = NULL;
  327.                     image_list_ptr->x = xd;
  328.                     image_list_ptr->y = yd;
  329.                 }
  330.             } else {
  331.                 McDbPrint ("LoadJpeg() : ");
  332.                 McPrint ("üª JPEGED.R é┼âGâëü[é¬ö¡É╢é╡é▄é╡é╜\n");
  333.             }
  334.             if (temp_image != NULL)
  335.                 _dos_mfree (temp_image);
  336.         } else {
  337.             McDbPrint ("LoadJpeg() : ");
  338.             McPrint ("üª .JPG ôWèJùpâüâéâèé¬æ½éΦé▄é╣é±\n");
  339.             _dos_mfree (image_list_ptr->data);
  340.             image_list_ptr->data = (void *) !NULL;
  341.         }
  342.         _dos_intvcs (0x170, old_MS_INIT);
  343.         _dos_intvcs (0x172, old_MS_CUROF);
  344.         _dos_intvcs (0x17d, old_SKEY_MOD);
  345.         _dos_intvcs (0xff23, old_CONCTRL);
  346.     } else {
  347.         /* Progressive JPEG é╠ÅΩìç */
  348.         McPrint ("üª âvâìâOâîâbâVâuJPEGé╔é═ûóæ╬ë₧é┼é╖\n");
  349.     }
  350.  
  351.     return (0);
  352. }
  353.  
  354.  
  355.  
  356. /* âCâüü[âWâèâXâgô╟é▌ì₧é▌ */
  357. /* QUIT_NON Ä₧î─é╤Åoé│éΩéΘâïü[â`âô */
  358. int LoadImage (void)
  359. {
  360.     HTTPFILE *httpfile;
  361.  
  362.     /* âXâ^âbâNâtâîü[âÇé⌐éτèmò█ */
  363.     if ((httpfile = alloca (sizeof (HTTPFILE))) == NULL) {
  364.         McDbPrint ("LoadImage() : ");
  365.         McPrint ("üª âüâéâèé¬æ½éΦé▄é╣é±\n");
  366.         return (-1);
  367.     }
  368.     image_list_ptr->x = 16;    /* ë╝é╔èäéΦôûé─éΘâTâCâY */
  369.     image_list_ptr->y = 16;
  370.     image_list_ptr->data = (void *) !NULL;    /* âfü[â^é¬û│éóÄ₧é╠âfâtâHâïâg */
  371.     strcpy (httpfile->url, image_list_ptr->url);
  372.  
  373.     if (GetFile (httpfile) < 0) {
  374.         McDbPrint ("LoadImage() : ");
  375.         McPrint (image_list_ptr->url);
  376.         McPrint ("é¬ô╟é▌ì₧é▀é▄é╣é±é┼é╡é╜\n");
  377.  
  378.         image_list_ptr->data = (void *) !NULL;    /* âfü[â^é¬û│éóÄ₧é╠âfâtâHâïâg */
  379.         /* free (httpfile); alloca é╔é┬é½òsùv */
  380.         return (-1);
  381.     } else {
  382.         McDbPrint ("LoadImage() : ");
  383.         McDbPrint (httpfile->url);
  384.         McDbPrint (" é≡Åêù¥é╡é▄é╖\n");
  385.         if (!strcmp (httpfile->content_type, "image/gif")) {
  386.             LoadGif (httpfile);
  387.         } else {
  388.             if (!strcmp (httpfile->content_type, "image/jpeg")) {
  389.                 LoadJpeg (httpfile);
  390.             }
  391.         }
  392.         _dos_mfree (httpfile->content);
  393.         /* free (httpfile); alloca é╔é┬é½òsùv */
  394.     }
  395.     return (0);
  396. }
  397.